package com.stable.actor.remotedemo;

import com.stable.actor.core.ActorRef;
import com.stable.actor.core.ActorSystem;
import com.stable.actor.core.Props;
import com.stable.actor.network.NetworkClient;
import lombok.extern.slf4j.Slf4j;
/**
 * 本地测试版本 - 使用localhost
 */
@Slf4j
public class LocalRemoteCallDemo {

    public static void main(String[] args) throws InterruptedException {
        log.info("🚀 启动本地远程调用测试...");

        ActorSystem system = new ActorSystem("LocalRemoteDemo");
        NetworkClient.initialize(system);

        try {
            // 创建本地服务
            ActorRef orderService = system.actorOf(Props.create(OrderServiceActor.class), "order-service");

            // 创建远程服务引用 - 使用localhost不同端口模拟多个服务
            log.info("🔗 创建本地远程服务引用...");

            // 三个服务都运行在本地，但使用不同端口
            ActorRef paymentService = system.remoteActorOf("localhost", 8081, "/user/payment-service");
            ActorRef inventoryService = system.remoteActorOf("localhost", 8082, "/user/inventory-service");
            ActorRef notificationService = system.remoteActorOf("localhost", 8083, "/user/notification-service");

            log.info("✅ 远程服务引用创建完成:");
            log.info("   - 支付服务: localhost:8081/user/payment-service");
            log.info("   - 库存服务: localhost:8082/user/inventory-service");
            log.info("   - 通知服务: localhost:8083/user/notification-service");

            // 设置远程服务
            OrderServiceActor orderActor = (OrderServiceActor) ((ActorRef) orderService).getActor();
            orderActor.setRemoteServices(paymentService, inventoryService, notificationService);

            // 测试订单
            log.info("\n📤 发送测试订单...");
            Order testOrder = new Order("TEST-001", "test-user", "Test Product", 1, 100.0);
            orderService.tell(new CreateOrderCommand(testOrder), ActorRef.noSender());

            Thread.sleep(5000);

        } finally {
            system.terminate();
            NetworkClient.shutdown();
        }
    }
}
